<?php

//Load QCubed Framework...
require_once('../../qcubed.inc.php');

//Load Custom Control
require_once('../shared/balloon.php');
require_once('../../admin/controls/QEditableLabel.class.php');


//Load Helpers
require_once('../../helpers/string_helper.php');
require_once('../../helpers/html_helper.php');

class Index extends QForm {

    protected $objCurrentUser;
    protected $objEditableArray;
    protected $objServiceArray;
    protected $objProjectArray;
    protected $objBalloon;
    protected $txtEmail;
    protected $txtMessage;
    protected $btnSend;

    protected function Form_Run() {
        parent::Form_Run();

        if (isset($_SESSION['signed_in'])) {
            $this->objCurrentUser = unserialize($_SESSION['user']);
        }
    }
    
    protected function Form_Create() {
        parent::Form_Create();

        $this->CssClass = 'form-mini';
        
        $objContentKeyArray = 
                ContentKey::LoadNameValueArrayByContentPageTypeId(ContentPageType::Home);
        
        foreach($objContentKeyArray as $key => $objContentKey){
            $this->objEditableArray[$key] = new QEditableLabel($this);
            $this->objEditableArray[$key]->Text = $objContentKey->Value;
            $this->objEditableArray[$key]->ActionParameter = $objContentKey->Id;
            $this->objEditableArray[$key]->ReadOnly = !isset($this->objCurrentUser);
            
            switch ($objContentKey->ContentKeyTypeId) {
                case ContentKeyType::SingleText:
                    $this->objEditableArray[$key]->TextMode = QTextMode::SingleLine;
                    break;
                case ContentKeyType::MultiText:
                    $this->objEditableArray[$key]->TextMode = QTextMode::MultiLine;
                    $this->objEditableArray[$key]->AddCssClass('grid_14');
                    $this->objEditableArray[$key]->Height = '150px';
                    break;
                case ContentKeyType::RichText:
                    $this->objEditableArray[$key]->TextMode = QTextMode::MultiLine;
                    $this->objEditableArray[$key]->AddCssClass('grid_14');
                    $this->objEditableArray[$key]->AddCssClass('wysihtml5');
                    $this->objEditableArray[$key]->Width = '100%';
                    $this->objEditableArray[$key]->Height = '150px';
                    break;
            }
        }

        $this->objServiceArray =
                Service::LoadAll(
                        QQ::Clause(
                                QQ::Select(QQN::Service()->Name, QQN::Service()->Description), QQ::OrderBy(QQN::Service()->Order), QQ::LimitInfo(10)));

        $this->objProjectArray =
                Project::LoadAll(
                        QQ::Clause(
                                QQ::Select(QQN::Project()->Name, QQN::Project()->Description), QQ::OrderBy(QQN::Project()->Date, true), QQ::LimitInfo(3)));

        

        $this->objBalloon = new BalloonControl($this);

        $this->txtEmail = new QEmailTextBox($this);
        $this->txtEmail->SetCustomAttribute('placeholder', QApplication::Translate('Email:'));
        
        $this->txtMessage = new QTextBox($this, 'message');
        $this->txtMessage->TextMode = QTextMode::MultiLine;
        $this->txtMessage->SetCustomAttribute('placeholder', QApplication::Translate('Mensaje:'));
        
        $this->txtEmail->Required =
        $this->txtMessage->Required = !isset($this->objCurrentUser);
        
        $this->txtEmail->LabelForRequiredUnnamed =
        $this->txtMessage->LabelForRequiredUnnamed = QApplication::Translate('* El campo es requerido.');

        $this->btnSend = new QButton($this);
        $this->btnSend->Text = 'enviar';
        $this->btnSend->CssClass = 'button';
        $this->btnSend->PrimaryButton = true;
        $this->btnSend->CausesValidation = true;
        $this->btnSend->AddAction(new QClickEvent(), new QAjaxAction('btnSend_Click'));
        $this->btnSend->AddAction(new QClickEvent(), new QTerminateAction());
    }

    protected function Form_Validate() {

        $blnToReturn = parent::Form_Validate();

        $blnFocused = false;
        foreach ($this->GetErrorControls() as $objControl) {

            if (!$blnFocused) {
                $objControl->Focus();
                $blnFocused = true;
            }

            $objControl->Blink();
        }

        if ($blnToReturn) {
            //QApplication::ExecuteJavaScript(sprintf("fcnBtnToggle('%s',true)", $this->btnSend->ControlId));
        }

        return $blnToReturn;
    }

    protected function btnSend_Click($strFormId, $strControlId, $strParameter) {
        try {
            QEmailServer::Send(
                    new QEmailMessage(
                            QEmailServer::$SmtpUsername,
                            __EMAIL_ADDRESS_TO__,
                            'Mensaje generado desde www.felixayala.com.ar',
                            $this->FormatEmailMessage()
                    )
            );

            //QApplication::ExecuteJavaScript(sprintf("fcnBtnToggle('%s',true)", $strControlId));
            $this->objBalloon->Success('Muchas Gracias!', 'Muy pronto nos pondremos en contacto con usted.');

            $this->txtEmail->Text =
            $this->txtMessage->Text = null;
            
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->objBalloon->Error('Oops!', $objExc->getMessage());
        }
    }

    private function FormatEmailMessage() {

        $body = __CONTACT_EMAIL_TPL__;
        $body = str_replace('{1}', $this->txtEmail->Text, $body);
        $body = str_replace('{3}', $this->txtMessage->Text, $body);

        return $body;
    }

}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/view/home/index.tpl.php');
?>